<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class Roles extends Model
{
    use SoftDeletes;
    protected $table = 'roles';
    protected $primaryKey = 'id';


    public function permissions()
    {
        return $this->belongsToMany('App\Models\Permissions', 'role_permission', 'role_id', 'permission_id');
    }


    public function users()
    {
        return $this->hasMany('App\Models\Admin', 'user_type');
    }

    public function getPids()
    {
        $ps = DB::table('role_permission')->where('role_id', $this->id)->get('permission_id');
        return Arr::pluck($ps, 'permission_id');
    }

    public function atctivUsers()
    {
        return DB::table('users')->where('user_type', $this->id)
            ->whereNull('deleted_at')
            ->where('status', 1)
            ->count('id');

    }

}
